<?php
// +----------------------------------------------------------------------
// | ThinkCMF [ WE CAN DO IT MORE SIMPLE ]
// +----------------------------------------------------------------------
// | Copyright (c) 2013-2017 http://www.thinkcmf.com All rights reserved.
// +----------------------------------------------------------------------
// | Author: pl125 <xskjs888@163.com>
// +----------------------------------------------------------------------

namespace api\admin\model;

use api\common\model\CommonModel;

class FeedbackTagModel extends CommonModel
{
    /**
     * [dataInsertTag 添加选项到数据库]
     * @param  [type] $tags [description]
     * @param  [type] $Fid  [description]
     * @return [type]       [description]
     */
    public function dataInsertTag($tags, $userId)
    {

        $result = 0;
        $ids    = [];

        $tags = array_filter($tags);
        
        foreach ($tags as $key => $value)
        {

            $exist = $this->where(['tag_name' => $value, 'user_id' => $userId, 'status' => 1])->find();

            if ($exist)
            {
                $result = 1;
                $ids[]  = $exist['id'];
                continue;
            }
            else
            {
                $result = $this->insert([
                    'create_time' => time(),
                    'user_id' => $userId,
                    'tag_name' => $value,
                ]);
                $ids[] = $this->getLastInsID();
            }
        }

        //删除多余数据
        if ($result)
        {

            $this->where([
                'user_id' => $userId,
                'id' => ['NOT IN', $ids],
            ])->update(['status' => 0]);

        }

        return $result;

    }



}
